package com.eee.xiang.market.service.impl;

import com.eee.xiang.market.beans.OperateResponse;
import com.eee.xiang.market.beans.TNotice;
import com.eee.xiang.market.beans.TNoticeComment;
import com.eee.xiang.market.beans.TUser;
import com.eee.xiang.market.dao.mapper.TNoticeCommentMapper;
import com.eee.xiang.market.dao.mapper.TNoticeMapper;
import com.eee.xiang.market.service.INoticeService;
import com.eee.xiang.market.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;


@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private HttpSession session;

    @Autowired
    private TNoticeMapper noticeMapper;
    @Autowired
    private TNoticeCommentMapper noticeCommentMapper;

    @Override
    public OperateResponse saveNotice(TNotice notice) {
        notice.setNoticeId(UUIDUtil.generate());
        int i = noticeMapper.insertSelective(notice);
        if(i > 0){
            return OperateResponse.obtainSuccessResponse(true);
        }else{
            return OperateResponse.failResponse("保存公告失败");
        }
    }

    @Override
    public OperateResponse saveNoticeComment(TNoticeComment noticeComment) {
        TUser user = (TUser)session.getAttribute("userInfo");
        noticeComment.setCommentId(UUIDUtil.generate());
        noticeComment.setCommentUser(user.getUserName());
        int i = noticeCommentMapper.insertSelective(noticeComment);
        if(i > 0){
            return OperateResponse.obtainSuccessResponse(true);
        }else{
            return OperateResponse.failResponse("吐槽失败");
        }
    }

    @Override
    public List<TNoticeComment> selectNoticeCommentList(String noticeId) {
        return noticeCommentMapper.selectNoticeCommentList(noticeId);
    }

    @Override
    public TNotice selectNotice(String noticeId) {
        return noticeMapper.selectByPrimaryKey(noticeId);
    }
}
